package me.shurufa.share;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.sina.weibo.sdk.api.ImageObject;
import com.sina.weibo.sdk.api.TextObject;
import com.sina.weibo.sdk.api.WebpageObject;
import com.sina.weibo.sdk.api.WeiboMessage;
import com.sina.weibo.sdk.api.share.IWeiboShareAPI;
import com.sina.weibo.sdk.api.share.SendMessageToWeiboRequest;
import com.sina.weibo.sdk.api.share.WeiboShareSDK;
import com.sina.weibo.sdk.utils.Utility;

import me.shurufa.utils.Constants;

/**
 * Created by kHRYSTAL on 2015/8/27.
 */
public class SinaShareManager{


    private static String sinaAppKey;
    Context context;
    public static final String SCOPE =
            "email,direct_messages_read,direct_messages_write,"
                    + "friendships_groups_read,friendships_groups_write,statuses_to_me_read,"
                    + "follow_app_official_microblog," + "invitation_write";
    /** 微博分享的接口实例 */
    private IWeiboShareAPI sinaAPI;

    public void registSina(Context context){
        //获取appkey
        if(sinaAppKey == null){
            sinaAppKey = Constants.SinaAppKey;
        }
        //初始化微博分享代码
        if(sinaAppKey != null){
            initSinaShare(context);
        }
    }

    /**
     * 新浪微博分享方法
     * @param shareContent 分享的内容
     */
    public void shareBySina(ShareContent shareContent, Context context){
        if(sinaAPI == null) return;
        this.context = context;
        switch (shareContent.getShareWay()) {
            case Constants.SINA_SHARE_WAY_TEXT:
                shareText(shareContent);
                break;
            case Constants.SINA_SHARE_WAY_PIC:
                sharePicture(shareContent, context);
                break;
            case Constants.SINA_SHARE_WAY_WEBPAGE:
                shareWebPage(shareContent, context);
                break;
        }
    }

    /*
     * 分享文字
     */
    private void shareText(ShareContent shareContent){
        //初始化微博的分享消息
        WeiboMessage weiboMessage = new WeiboMessage();
        weiboMessage.mediaObject = getTextObj(shareContent.getContent());
        //初始化从第三方到微博的消息请求
        SendMessageToWeiboRequest request = new SendMessageToWeiboRequest();
        request.transaction = buildTransaction("sinatext");
        request.message = weiboMessage;
        //发送请求信息到微博，唤起微博分享界面
        sinaAPI.sendRequest((Activity)context,request);
    }

    /*
     * 分享图片
     */
    private void sharePicture(ShareContent shareContent, Context context){
        WeiboMessage weiboMessage = new WeiboMessage();
        weiboMessage.mediaObject = getImageObj(shareContent.getPicResource(), context);
        //初始化从第三方到微博的消息请求
        SendMessageToWeiboRequest request = new SendMessageToWeiboRequest();
        request.transaction = buildTransaction("sinatext");
        request.message = weiboMessage;
        //发送请求信息到微博，唤起微博分享界面
        sinaAPI.sendRequest((Activity)context,request);
    }

    private void shareWebPage(ShareContent shareContent, Context context){
        WeiboMessage weiboMessage = new WeiboMessage();
        weiboMessage.mediaObject = getWebpageObj(shareContent, context);
        //初始化从第三方到微博的消息请求
        SendMessageToWeiboRequest request = new SendMessageToWeiboRequest();
        request.transaction = buildTransaction("sinatext");
        request.message = weiboMessage;
        //发送请求信息到微博，唤起微博分享界面
        sinaAPI.sendRequest((Activity)context,request);
    }


    /**
     * 创建文本消息对象。
     *
     * @return 文本消息对象。
     */
    private TextObject getTextObj(String text) {
        TextObject textObject = new TextObject();
        textObject.text = text;
        return textObject;
    }

    private ImageObject getImageObj(Bitmap picResource, Context context){
        ImageObject imageObject = new ImageObject();

        imageObject.setImageObject(picResource);
        return imageObject;
    }

    private WebpageObject getWebpageObj(ShareContent shareContent, Context context){
        WebpageObject mediaObject = new WebpageObject();
        mediaObject.identify = Utility.generateGUID();
        mediaObject.title = shareContent.getTitle();
        mediaObject.description = shareContent.getContent();

        // 设置 Bitmap 类型的图片到视频对象里

        mediaObject.setThumbImage(shareContent.getPicResource());
        mediaObject.actionUrl = shareContent.getURL();
        mediaObject.defaultText = shareContent.getContent();
        return mediaObject;
    }

    private void initSinaShare(Context context){
        // 创建微博 SDK 接口实例
        sinaAPI = WeiboShareSDK.createWeiboAPI(context, sinaAppKey);
        //检查版本支持情况
        checkSinaInstalled();
        sinaAPI.registerApp();
    }

    private boolean checkSinaInstalled() {
        boolean isInstalledWeibo = sinaAPI.isWeiboAppInstalled();
        return isInstalledWeibo;
    }

    private String buildTransaction(final String type) {
        return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
    }
}
